<% layout("/inc/layout_page.html"){ %>
<div class="box box-search box-solid">
  <div class="box-header">
    <i class="fa fa-ellipsis-v"></i>
    <h3 class="box-title">查询</h3>
    <div class="pull-right box-tools">
      <span onclick="reload_table()" class="btn btn-info btn-sm"><i class="fa fa-search"></i> 查询</span>
      <span onclick="form_search_reset()" class="btn btn-info btn-sm"><i class="fa fa-refresh"></i> 重置</span>
      <button class="btn btn-sm" data-widget="collapse"><i class="fa fa-minus"></i></button>
    </div>
  </div>
  <div class="box-body">
    <form id="form_search">
      <input type="text" style="width:160px" placeholder="标题" id="_s_title" name="_s_title" />
      <input type="hidden" id="_s_columnId" name="_s_columnId" />
    </form>
  </div>
</div>

<div class="row">
<div class="col-xs-3">
<div class="box">
  <div class="box-header with-border"><h4 class="box-title"><i class="fa fa-th-list"></i> 栏目树</h4></div>
  <div class="box-body"><div style="overflow-x:auto;"><ul id="ul_menu_select_tree" class="ztree"></ul></div></div>
</div>
</div>
<div class="col-xs-9">

<form action="${BASE}/admin/cms/CmsArticle/updateSort" id="form_updateSort" method="post"> 
<div class="box">
  <div class="box-header">
    <h4 class="box-title"><i class="fa fa-bars"></i> 文章列表</h4>
    <div class="box-tools">
      <span onclick="list_add()" class="btn btn-info btn-sm"><i class="fa fa-plus-square"></i> 添加文章</span>
      <button type="submit" class="btn btn-success btn-sm"><i class="fa fa-sort-numeric-desc"></i> 保存排序</button>
    </div>
  </div>
  <div class="box-body no-padding">
    <table id="table_list" class="table table-border table-bordered table-bg table-striped table-hover">
      <thead>
        <tr class="text-c">
          <th>所属栏目</th>
          <th>文章标题</th>
          <th width="60">排序</th>
          <th>点击数</th>
          <th>回复数</th>
          <th width="170">操作</th>
        </tr>
      </thead>
      <tbody></tbody>
    </table>
  </div>
</div>
</form>

</div>
</div>

<script type="text/javascript">
var _table;
function reload_table(){
	if(_table){
		_table.draw();
	}else{
		alert("没有表格");
	}
}

function list_add(){
	var cid = $("#_s_columnId").val();
	var index = layer.open({
		type: 2,
		closeBtn: 0,
		title: false,
		area: ['98%', '98%'],
		content: "${BASE}/admin/cms/CmsArticle/add?pid="+cid
	});
	layer.full(index);
}

function list_del(id){
	layer.confirm('确认删除所选记录?', {icon: 3, title:'警告'}, function(index){  
	    layer.close(index);
	    layer.load(1, {shade: [0.3,'#fff']});
	    $.post("${BASE}/admin/cms/CmsArticle/delete",{ids:id},function(jsondata){
			layer.closeAll('loading');
			if( jsondata.code == "001" ){
				toastr.success('删除成功！');
				reload_table();
			}else{
				toastr.error(jsondata.msg);
			}
	    },"json");
	});
}
function list_edit(id){
	var index = layer.open({
		type: 2,
		closeBtn: 0,
		title: false,
		area: ['98%', '98%'],
		content: "${BASE}/admin/cms/CmsArticle/edit?id="+id
	});
	layer.full(index);
}
function list_show(id){
    var index = layer.open({
		type: 2,
		closeBtn: 1,
		title: "文章表",
		area: ['98%', '98%'],
		content: "${BASE}/admin/cms/CmsArticle/show?id="+id
	});
}
function form_search_reset() {
	$("#form_search")[0].reset();
	menuTree.cancelSelectedNode();
	$("#_s_columnId").val("");
	reload_table();
}


var menuTree;
var tree_setting = {
	callback: {
		onClick: function(event, treeId, treeNode){
			$("#_s_columnId").val(treeNode.id);
			reload_table();
		}
	}
};

$(function(){
	$.getJSON("${BASE}/admin/cms/CmsColumns/treeData",{}, function(str){
		$("#ul_menu_select_tree").text("");
		if(str)
			menuTree = $.fn.zTree.init($("#ul_menu_select_tree"), tree_setting, str);
		else
			$("#ul_menu_select_tree").html("<li>没有栏目数据</li>");
	});
	
	_table = $("#table_list").DataTable({
        language: {
            "url": "/lib/datatables/Chinese.json"
        },
	    dom: 'rtlip',
		autoWidth: false,
		scrollX: true,
		ordering:false,
		searching:false,
		serverSide:true,
		ajax: {
		    url: "${BASE}/admin/cms/CmsArticle/list",
		    type: "POST",
		    data: function ( d ) {
		        d._s_title = $('#_s_title').val();
		        d._s_columnId = $('#_s_columnId').val();
	        }
		},
        columns: [
            { data: "tails.colName",defaultContent:"" },
            { data: "title",defaultContent:"" },
            { data: "sort",class:"text-c", render: function(data, type, row) {
            	return '<input type="hidden" name="ids" value="'+row.id+'"/>'
    	  		+'<input name="sorts" type="text" value="'+data+'" style="width:60px">';
    	  	}},
            { data: "hits",class:"text-c",defaultContent:"" },
            { data: "tails.commentNumber",class:"text-c",defaultContent:"" },
            { data: "id",class:"text-c", render: function(data, type, row) {
            	return '<a class="btn btn-success btn-xs" href="javascript:;" title="查看" onclick="list_show(\''+data+'\')"><i class="fa fa-file-text-o"></i> 查看</a>&nbsp;'
            	  		+'<a class="btn btn-primary btn-xs" href="javascript:;" title="编辑" onclick="list_edit(\''+data+'\')"><i class="fa fa-pencil-square-o"></i> 编辑</a>&nbsp;'
            	  		+'<a class="btn btn-danger btn-xs" href="javascript:;" title="删除" onclick="list_del(\''+data+'\')"><i class="fa fa-trash-o"></i> 删除</a>';
            }}
        ]
	});
	
	$("#form_updateSort").validate({
		errorPlacement: function(error, element) {
			error.appendTo(element.parent());
		},
		submitHandler : function(form) {
			layer.load(1, {shade: [0.3,'#fff']});
			$(form).ajaxSubmit({
				dataType:"json",
				success:function( jsondata ){
					layer.closeAll('loading');
					if(jsondata.success){
						toastr.success('成功！');
					}else{
						toastr.error('出错了！');
					}
				}
			});
		},
		rules : {
			sorts : {
				digits : true,
				min:0
			}
		}, 
		messages : { 
			sorts : { 
				digits : "只能为整数值",
				min : "最小值为0"
			}
		}
	});
})

</script>
<% } %>